import 'package:flutter/material.dart';
import 'package:huajianshe_app/base/BaseState.dart';
import 'package:huajianshe_app/common/C.dart';
import 'package:huajianshe_app/common/load_image.dart';
import 'package:huajianshe_app/widget/RoundedCornerWidget.dart';
import 'package:huajianshe_app/widget/SmartButton.dart';

class BottomNavWidget extends StatefulWidget {

  final List<String> selectedUrls;
  final List<String> unselectedUrls;
  final List<String> titles;
  final EdgeInsets padding;
  ValueNotifier<int>? currentIndex;

  BottomNavWidget({Key? key,
    this.selectedUrls = const [],
    this.unselectedUrls = const [],
    this.titles = const [],
    this.padding = EdgeInsets.zero,
    this.currentIndex}) : super(key: key);

  @override
  State<StatefulWidget> createState() {
    return _BottomNavWidgetState();
  }
}

class _BottomNavWidgetState extends BaseState<BottomNavWidget> {

  @override
  void initState() {
    super.initState();
    if (widget.currentIndex == null) {
      widget.currentIndex = ValueNotifier(0);
    }
  }

  @override
  Widget build(BuildContext context) {
    return SafeArea(top: false, child: Padding(padding: widget.padding, child:
      RoundedCornerWidget(backgroundColor: Colors.white, radius: 30, width: double.infinity, height: 60, boxShadowList: [
        BoxShadow(color: Color.fromRGBO(0x35, 0x49, 0x5D, 0.2), blurRadius: 20.0, spreadRadius: 0.0),
      ], child:
        Row(mainAxisAlignment: MainAxisAlignment.spaceEvenly, children: [
          ...buildMenus()
        ])
      )
    ));

  }

  List<Widget> buildMenus() {
    List<Widget> result = [];
    for (int i = 0; i < widget.titles.length; i++) {
      // GestureDetector(
      //   child: SizedBox(width: 40,),
      // )

      result.add(SmartButton(
        onPressed: () {
          print('$i pressed');
          widget.currentIndex?.value = i;
          setState(() { });
          // widget.currentIndex?.notifyListeners();
        },
        backgroundColor: Colors.transparent,
        borderColor: Colors.transparent,
        borderWidth: 0,
        text: '${widget.titles[i]}',
        textColor: widget.currentIndex?.value == i ? Colors.black : C.gray999,
        fontSize: 10,
        width: 44,
        height: 58,
        drawableTop: LoadImage('${widget.currentIndex?.value == i ? widget.selectedUrls[i] : widget.unselectedUrls[i]}') ,
      ));
    }
    return result;
  }
}